import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Page } from 'src/domain/Page';
import { User } from 'src/domain/User';
import { Response } from '../../../domain/Response';
import {CacheService} from '../localstorage/cache.service';
import {LOADING_MESSAGE} from '../localstorage/cache.service.namespace';

@Injectable({
  providedIn: 'root'
})
export class UserApiService {
  constructor(private http: HttpClient, private cache: CacheService) {}

  /**
   * 用户列表
   * @param {number} pageSize
   * @param {number} offset
   * @returns {Observable<Response<Page<User>>>}
   */
  public users(
    pageSize: number,
    offset: number
  ): Observable<Response<Page<User>>> {
    return this.http.get<Response<Page<User>>>(
      '/admin/users?pageSize=' + pageSize + '&offset=' + offset
    );
  }

  /**
   * 创建用户
   * @param {User} user
   * @returns {Observable<Response<User>>}
   */
  public createUser(user: User): Observable<Response<User>> {
    this.cache.set(LOADING_MESSAGE,{url:'/admin/signUp', message: '正在创建用户....'});
    return this.http.post<Response<User>>('/admin/signUp',user)
  }

  /**
   * 修改用户状态
   * @param {number} id
   * @returns {Observable<Response<User>>}
   */
  public userStatus(id: number): Observable<Response<User>> {
    this.cache.set(LOADING_MESSAGE,{url:'/admin/user/', message:'修改用户状态中....'});
    return this.http.put<Response<User>>(`/admin/user/${id}`,{});
  }
}
